package org.locker.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.PaySplitBo;
import org.locker.domain.vo.PaySplitVo;
import org.locker.domain.PaySplit;
import org.locker.mapper.PaySplitMapper;
import org.locker.service.IPaySplitService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信分账Service业务层处理
 *
 * @author winnie
 * @date 2024-01-25
 */
@RequiredArgsConstructor
@Service
public class PaySplitServiceImpl  extends ServiceImpl<PaySplitMapper,PaySplit>  implements IPaySplitService {

    private final PaySplitMapper baseMapper;

    /**
     * 查询微信分账
     */
    @Override
    public PaySplitVo queryById(Long paySplitId){
        return baseMapper.selectVoById(paySplitId);
    }

    /**
     * 查询微信分账列表
     */
    @Override
    public TableDataInfo<PaySplitVo> queryPageList(PaySplitBo bo, PageQuery pageQuery) {
        Page<PaySplitVo> list = baseMapper.list(pageQuery.build(), bo);
        return TableDataInfo.build(list);
    }

    /**
     * 查询微信分账列表
     */
    @Override
    public List<PaySplitVo> queryList(PaySplitBo bo) {
        LambdaQueryWrapper<PaySplit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PaySplit> buildQueryWrapper(PaySplitBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PaySplit> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), PaySplit::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), PaySplit::getOrderNo, bo.getOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOutOrderNo()), PaySplit::getOutOrderNo, bo.getOutOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), PaySplit::getState, bo.getState());
        lqw.eq(bo.getCreateTime() != null, PaySplit::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增微信分账
     */
    @Override
    public Boolean insertByBo(PaySplitBo bo) {
        PaySplit add = MapstructUtils.convert(bo, PaySplit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPaySplitId(add.getPaySplitId());
        }
        return flag;
    }

    /**
     * 修改微信分账
     */
    @Override
    public Boolean updateByBo(PaySplitBo bo) {
        PaySplit update = MapstructUtils.convert(bo, PaySplit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PaySplit entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信分账
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
